<?php
//图像相关
class lib_image extends Image_GD{
	private $pic;
	
	public static function instance($pic){
		if (is_file($pic)){
			$obj = new self($pic);
			$obj->pic = $pic;
			return $obj;
		}
		return false;
	}
	
	//根据配置文件定义的图像重采样种类进行另存为
	public function imgResize(){
		$dir = dirname($this->pic);
		$name = common::fileprename($this->pic);
		$ext = common::fileext($this->pic);
		
		$type = project::config('libconfig')->imgSize;
		asort($type);
		$type = array_reverse($type);
		
		$nameRule = project::config('libconfig')->imgSize_nameRule;
		
		$resized = array();
		foreach ($type as $size=>$param){
			call_user_func_array(array($this,'resize'),$param);
			$new_name = str_replace(
				array('%name','%size','%width','%height','%ext'),
				array($name,$size,$param[0],$param[1],$ext),
				$nameRule[$size]
			);
			$resized[$this->pic][$size] = $new_pic = $dir.'/'.$new_name;
			$this->save($new_pic);
		}
		@unlink($this->pic);
		return $resized;
	}
	
	//根据图片原型名称和尺寸参数，返回真实的文件名
	public static function img_by_size($pic,$size){
		$dir = dirname($pic);
		$name = common::fileprename($pic);
		$ext = common::fileext($pic);
		
		$type = project::config('libconfig')->imgSize;
		$nameRule = project::config('libconfig')->imgSize_nameRule;
		$real_name = str_replace(
			array('%name','%size','%width','%height','%ext'),
			array($name,$size,$type[$size][0],$type[$size][1],$ext),
			$nameRule[$size]
		);
		$real_pic = $dir.'/'.$real_name;
		return $real_pic;
	}
}